// 渲染会议举办地址
function renderCity() {
    var json = [
        {
            "code": "1",
            "value": "上海"
        },
        {
            "code": "2",
            "value": "北京"
        },
        {
            "code": "3",
            "value": "广州"
        }
    ]
    var frment = document.createDocumentFragment();
    json.forEach((item) => {
        var ele = document.createElement('li')
        ele.setAttribute('data-value', item.value);
        ele.setAttribute('data-code', item.code);
        ele.className = 'select-option J_option';
        ele.innerHTML = item.value;
        frment.appendChild(ele);
    })
    document.getElementById('cityTypeId').appendChild(frment);
}

// 渲染公司会议类型
function renderMetting() {
    var json = [
        {
            "code": "1",
            "value": "年会"
        },
        {
            "code": "2",
            "value": "团建"
        },
        {
            "code": "3",
            "value": "旅游"
        }
    ]
    var frment = document.createDocumentFragment();
    json.forEach((item) => {
        var ele = document.createElement('li');
        ele.setAttribute('data-value', item.value);
        ele.setAttribute('data-code', item.code);
        ele.className = 'select-option J_option';
        ele.innerHTML = item.value;
        frment.appendChild(ele);
    })
    document.getElementById('meetTypeId').appendChild(frment);
}

// 渲染活动时长
function renderActivityTime() {
    var json = [
        {
            "code": "1",
            "value": "一晚"
        },
        {
            "code": "2",
            "value": "半天"
        },
        {
            "code": "3",
            "value": "一天"
        },
        {
            "code": "4",
            "value": "两天"
        },
        {
            "code": "5",
            "value": "3-5天"
        }
    ]
    var frment = document.createDocumentFragment();
    json.forEach((item) => {
        var ele = document.createElement('li')
        ele.setAttribute('data-value', item.value);
        ele.setAttribute('data-code', item.code);
        ele.className = 'select-option J_option';
        ele.innerHTML = item.value;
        frment.appendChild(ele);
    })
    document.getElementById('activityTimeId').appendChild(frment);
}

//参会人数
function renderOnMeetNumber() {
    var json = [
        {
            "code": "1",
            "value": "10-30人"
        },
        {
            "code": "2",
            "value": "30-60人"
        },
        {
            "code": "3",
            "value": "60-100人"
        },
        {
            "code": "4",
            "value": "100-200人"
        },
        {
            "code": "5",
            "value": "200-300人"
        }
    ]
    var frment = document.createDocumentFragment();
    json.forEach((item) => {
        var ele = document.createElement('li')
        ele.setAttribute('data-value', item.value);
        ele.setAttribute('data-code', item.code);
        ele.className = 'select-option J_option';
        ele.innerHTML = item.value;
        frment.appendChild(ele);
    })
    document.getElementById('onMeetNumberId').appendChild(frment);
}

//大致预算
function renderMeetBudget() {
    var json = [
        {
            "code": "1",
            "value": "3000一下"
        },
        {
            "code": "2",
            "value": "3-5千"
        },
        {
            "code": "3",
            "value": "5-8千"
        },
        {
            "code": "4",
            "value": "1万-5万"
        },
        {
            "code": "5",
            "value": "5万以上"
        }
    ]
    var frment = document.createDocumentFragment();
    json.forEach((item) => {
        var ele = document.createElement('li')
        ele.setAttribute('data-value', item.value);
        ele.setAttribute('data-code', item.code);
        ele.className = 'select-option J_option';
        ele.innerHTML = item.value;
        frment.appendChild(ele);
    })
    document.getElementById('meetBudgetId').appendChild(frment);
}

//需求类型
function renderMeetDemand() {
    var json = [
        {
            "code": "1",
            "value": "开会"
        },
        {
            "code": "2",
            "value": "聚餐"
        },
        {
            "code": "3",
            "value": "年会"
        }
    ]
    var frment = document.createDocumentFragment();
    json.forEach((item) => {
        var ele = document.createElement('li')
        ele.setAttribute('data-value', item.value);
        ele.setAttribute('data-code', item.code);
        ele.className = 'select-option J_option';
        ele.innerHTML = item.value;
        frment.appendChild(ele);
    })
    document.getElementById('meetDemandId').appendChild(frment);
}

let hotelImage ;




//合作酒店轮播图
function cooperateHotel() {
    let fragment = document.createDocumentFragment();
    $.ajax({
        type: "GET",
        url: 'http://bo.huiyibeibei.com/api/mtbb/getCity',
        success: function (result) {
            if (result.code === 200) {
                let data = result.data;
                hotelImage = data;
                creatImageElement(hotelImage[0].homeCityDetails, fragment);
                document.getElementById('cooperateHotelId').appendChild(fragment);
                startSlider()
            }

        }
    });
}

//根据城市获取所在酒店
function getCityImage(e) {
    $('#cooperateHotelId').removeChild()
    let elementById = document.getElementById('cooperateHotelId');
    while (elementById.hasChildNodes()) //当div下还存在子节点时 循环继续
    {
        elementById.removeChild(elementById.firstChild);
    }
    let innerText = $(e).context.innerText;
    let fragment = document.createDocumentFragment();
    hotelImage.forEach((item) => {
        if (innerText === item.city) {
            let homeCityDetails = item.homeCityDetails;
            creatImageElement(homeCityDetails, fragment);
        }

    });
    document.getElementById('cooperateHotelId').appendChild(fragment);
    startSlider()
}


function creatImageElement(value, fragment) {
    value.forEach((item) => {
        let ele = document.createElement('div');
        ele.addEventListener('click', function () {
            window.location.href = "../html/hotelDetail.html?hotelId=" + item.id;
        });
        ele.className = 'box';
        ele.style = "background-image: url(" + item.fileUrl + ")"
        //第二层子元素
        let divChild = document.createElement('div');
        divChild.className = 'shadow';

        //a链接
        let a = document.createElement('a');
        a.href = '#';
        a.innerHTML = item.desc;
        divChild.appendChild(a);
        ele.appendChild(divChild)
        fragment.appendChild(ele);
    })
}


//举办会议
function holdMeeting() {
    let fragment = document.createDocumentFragment();

    $.ajax({
        type: "GET",
        url: 'http://bo.huiyibeibei.com/api/mtbb/getMeeting',
        success: function (result) {
            if (result.code ===200){
                result.data.forEach((item) => {

                  /*  let ele = document.createElement('div');
                    ele.className = 'main-common-wrap recent-warp';
                    fragment.appendChild(ele);*/
                   /* let ul = document.createElement('ul');
                    ul.className = 'whither-cont-list J_whitherCont';*/
                    let li = document.createElement('li');
                    li.className = 'whither-cont-li case caseindex ';

                    let div = document.createElement('div');
                    div.className = 'whither-img';
                    li.appendChild(div);
                    let img = document.createElement('img');
                    img.className = 'lazyload';
                    img.setAttribute('data-code', item.id);
                    img.src = item.fileUrl;
                    img.alt = item.name;
                    img.setAttribute('data-value', item.fileUrl);
                    div.appendChild(img);

                    let div2 = document.createElement('div');
                    div2.className = 'whiter-li-title';
                    div2.innerHTML = item.name;
                    li.appendChild(div2);
                    let div3 = document.createElement('div');
                    div3.className = 'whiter-hotel-desc';
                    div3.innerHTML = item.detail;
                    li.appendChild(div3);

                    //ul.appendChild(li);
                    fragment.appendChild(li)
                })
                document.getElementById('holdHotelId').appendChild(fragment);
            }
        }
    });

}


function submitOrder() {
    let ele = document.getElementsByClassName('J_value');
    if (ele[0].getAttribute('data-value') === ""){
        $(".select-pull").css({display: "block"});
        return
    }
    if (document.getElementById("phoneId").value === ""){
       document.getElementById('phoneId').className ='input-wrap-error  home-page fs15';
        return;
    }
    if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(document.getElementById("phoneId").value))){
        popup.show({
            type: 'confirm',
            text: '手机号可能不合法',
            callback: function () {
            }
        });
        return;
    }


    if (document.getElementById("contactNameId").value === ""){
        document.getElementById('contactNameId').className ='input-wrap-error  home-page fs15';
        return;
    }

    let data = {
        "address":ele[0].innerText,
        "meetingTime": ele[1].innerText,
        "numOfPeople": ele[2].innerText,
        "meetingType": ele[3].innerText,
        "budget": ele[4].innerText,
        "meetingDetail": ele[5].innerText,
        "phone": $("#phoneId").val(),
        "contactName": $("contactNameId").val(),
        "channel": 'pc'
    }
    $.ajax({
        url: "http://bo.huiyibeibei.com/api/mtbb/insertAppointment",
        type: "POST",
        dataType: "JSON",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify(data)
    }).done(function (e) {
        if (e.code=== 200){
            thisAlert(e.message);
        }else{
            popup.show({
                type: 'confirm',
                text: e.message,
                callback: function () {
                }
            });
        }
    })

}

function thisAlert(message){
    popup.show({
        type: 'confirm',
        text: message,
        callback: function () {
            location.reload();
        }
    });
}
